using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

using System.Collections;
using System.ComponentModel;
using System.Security;
using System.Security.Permissions;

namespace CaveArt.Common.Util.IPMonitor
{
    [XmlRoot]
    public class Configuration
    {
        private const string GridCategory01 = "1. IP Configuration";
        private const string GridCategory02 = "2. Email Configuration";
        private const string GridCategory03 = "3. Etc.";

        private const string DefaultQueryURL = "http://checkip.dyndns.org/";
        private const string DefaultToEmail = "someone@somewhere.com";
        private const string DefaultFromEmail = "server@somewhere.com";
        private const string DefaultEmailSubject = "My IP address has been changed.";
        private const string DefaultSMTPServerAddress = "127.0.0.1";
        private const int DefaultSMTPTimeout = 60; // seconds
        private const int DefaultSMTPPort = 25;
        private const bool DefaultSMTPSSL = false;
        private const int DefaultPollingMinutes = 60;
        private const string DefaultUserID = "My email server ID";
        private const string DefaultPassword = "My email server password";

        private string _QueryURL;
        private string _ToEmail;
        private string _FromEmail;
        private string _EmailSubject;
        private string _SMTPServerAddress;
        private string _ExternalIP;
        private int _SMTPTimeout;
        private int _SMTPPort;
        private int _PollingMinutes;
        private bool _SMTPEnableSSL;
        private string _UserID;
        private string _Password;

        #region Properties
        [
        XmlAttribute,
        Category(GridCategory01),
        //DefaultValue(DefaultQueryURL),
        DisplayName("Query URL"),
        Description("URL to verify your external IP address. Default URL is http://checkip.dyndns.org/"),
        CaveArt.UIcomp.PropertyOrder(10)
        ]
        public string QueryURL
        {
            get { return _QueryURL; }
            set { _QueryURL = value; }
        }

        [
        XmlAttribute,
        Category(GridCategory01),
        //DefaultValue("Unknown"),
        ReadOnly(true),
        DisplayName("Current IP Address"),
        Description("Your current external IP address."),
        CaveArt.UIcomp.PropertyOrder(20)
        ]
        public string ExternalIP
        {
            get { return _ExternalIP; }
            set { _ExternalIP = value; }
        }

        [
        XmlAttribute,
        Category(GridCategory02),
        //DefaultValue(DefaultToEmail),
        DisplayName("Email To"),
        Description("Email address to send notification of your IP address change. You can specify multiple email addresses like 'someone1@somewhere.com;someone2@elsewhere.com'."),
        CaveArt.UIcomp.PropertyOrder(30)
        ]
        public string ToEmail
        {
            get { return _ToEmail; }
            set { _ToEmail = value; }
        }

        [
        XmlAttribute,
        Category(GridCategory02),
        //DefaultValue(DefaultFromEmail),
        DisplayName("Email From"),
        Description("Email address of sender. The notification email will be sent with this email address."),
        CaveArt.UIcomp.PropertyOrder(40)
        ]
        public string FromEmail
        {
            get { return _FromEmail; }
            set { _FromEmail = value; }
        }

        [
        XmlAttribute,
        Category(GridCategory02),
        //DefaultValue(DefaultEmailSubject),
        DisplayName("Email Subject"),
        CaveArt.UIcomp.PropertyOrder(50)
        ]
        public string EmailSubject
        {
            get { return _EmailSubject; }
            set { _EmailSubject = value; }
        }

        [
        XmlIgnore,
        Browsable(true),
        Category(GridCategory02),
        //DefaultValue(DefaultSMTPServerAddress),
        DisplayName("SMTP Server"),
        Description("Email server IP address or internet domain address. (ex). '123.45.210.99' or 'myemail.myemailserver.com'"),
        CaveArt.UIcomp.PropertyOrder(60)
        ]
        public string UISMTPServerAddress
        {
            get { return (_SMTPServerAddress.Length == 0) ? String.Empty : Decode(_SMTPServerAddress); }
            set { _SMTPServerAddress = (value.Length == 0) ? String.Empty : Encode(value); }
        }

        [
        XmlAttribute,
        Browsable(false)
        ]
        public string SMTPServerAddress
        {
            get { return _SMTPServerAddress; }
            set { _SMTPServerAddress = value; }
        }

        [
        XmlAttribute,
        Category(GridCategory02),
        DefaultValue(DefaultSMTPTimeout),
        DisplayName("SMTP Timeout"),
        Description("SMTP server timeout for seconds (default=60 seconds)."),
        CaveArt.UIcomp.PropertyOrder(61)
        ]
        public int SMTPTimeout
        {
            get { return _SMTPTimeout; }
            set { _SMTPTimeout = value; }
        }

        [
        XmlAttribute,
        Category(GridCategory02),
        DefaultValue(DefaultSMTPPort),
        DisplayName("SMTP Port"),
        Description("SMTP sever port (default = 25)."),
        CaveArt.UIcomp.PropertyOrder(62)
        ]
        public int SMTPPort
        {
            get { return _SMTPPort; }
            set { _SMTPPort = value; }
        }

        // EnableSSL
        [
        XmlAttribute,
        Category(GridCategory02),
        DefaultValue(DefaultSMTPSSL),
        DisplayName("Enable SSL"),
        Description("Enable or disable SSL (default = No)."),
        CaveArt.UIcomp.PropertyOrder(63)
        ]
        public bool SMTPEnableSSL
        {
            get { return _SMTPEnableSSL; }
            set { _SMTPEnableSSL = value; }
        }

        [
        XmlIgnore,
        Browsable(true),
        Category(GridCategory02),
        //DefaultValue(DefaultUserID),
        DisplayName("SMTP Server Login ID"),
        Description("Email server login ID. SMTP based email server has login ID. This ID can be used to authorize email send/receiving activities."),
        CaveArt.UIcomp.PropertyOrder(70)
        ]
        public string UIUserID
        {
            get { return (_UserID.Length == 0) ? String.Empty : Decode(_UserID); }
            set { _UserID = (value.Length == 0) ? String.Empty : Encode(value); }
        }

        [
        XmlAttribute,
        Browsable(false)
        ]
        public string UserID
        {
            get { return _UserID; }
            set { _UserID = value; }
        }

        [
        XmlIgnore,
        Browsable(true),
        Category(GridCategory02),
        //DefaultValue(""),
        DisplayName("Email Password"),
        Description("Email server login password."),
        CaveArt.UIcomp.PropertyOrder(80),
        Editor(typeof(CaveArt.UIcomp.PasswordUITypeEditor), typeof(System.Drawing.Design.UITypeEditor)),
        TypeConverter(typeof(CaveArt.UIcomp.PasswordTypeConverter))
        ]
        public string UIPassword
        {
            get { return _Password.Length == 0 ? String.Empty : Decode(_Password); }
            set { _Password = value.Length == 0 ? String.Empty : Encode(value); }
        }

        [
        XmlAttribute,
        Browsable(false),
        ]
        public string Password
        {
            get { return _Password; }
            set { _Password = value; }
        }
        
        [
        XmlAttribute,
        Category(GridCategory03),
        //DefaultValue(DefaultPollingMinutes),
        DisplayName("Polling Minutes"),
        ReadOnly(true),
        Description("Reserved. Not used currently."),
        CaveArt.UIcomp.PropertyOrder(90)
        ]
        public int PollingMinutes
        {
            get { return _PollingMinutes; }
            set { _PollingMinutes = value; }
        }
        #endregion

        public void Default()
        {
            // no settings file found, load defaults
            _QueryURL = DefaultQueryURL;
            _ToEmail = DefaultToEmail;
            _FromEmail = DefaultFromEmail;
            _EmailSubject = DefaultEmailSubject;
            _SMTPServerAddress = Encode(DefaultSMTPServerAddress);
            _SMTPPort = DefaultSMTPPort;
            _SMTPTimeout = DefaultSMTPTimeout;
            _SMTPEnableSSL = DefaultSMTPSSL;
            _PollingMinutes = DefaultPollingMinutes;

            _UserID = Encode(DefaultUserID);
            _Password = Encode(DefaultPassword);
        }

        private string Encode(string str)
        {
            return Crypto.Encrypt(str);
        }

        private string Decode(string str)
        {
            return Crypto.Decrypt(str);
        }
    }
}
